package handler

import (
	"net/http"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type addDirectMathodTransactionParams struct {
	ID      int64  `json:"employee_id" binding:"required"`
	Bank    string `json:"bank" binding:"required"`
	Account string `json:"account" binding:"required"`
}

func (h *Handler) AddDirectMathodTransaction(ctx *gin.Context) {
	var req addDirectMathodTransactionParams
	// Bind incoming json to struct and check for validation errors
	if ok := bindData(ctx, &req); !ok {
		return
	}

	d := &model.DirectMethod{
		Bank:    req.Bank,
		Account: req.Account,
	}

	if err := h.DirectMethodService.Create(ctx, req.ID, d); err != nil {
		zap.L().Error("server create direct method failed", zap.Error(err))
		e := apperrors.NewInternal()
		ctx.JSON(e.Status(), gin.H{
			"msg": e.Message,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": createPaymentMethodSuccess,
	})
}

type changeDirectMathodTransactionParams struct {
	ID      int64  `json:"employee_id" binding:"required"`
	Bank    string `json:"bank" binding:"required"`
	Account string `json:"account" binding:"required"`
}

func (h *Handler) ChangeDirectMathodTransaction(ctx *gin.Context) {
	var req changeDirectMathodTransactionParams
	// Bind incoming json to struct and check for validation errors
	if ok := bindData(ctx, &req); !ok {
		return
	}

	d := &model.DirectMethod{
		Bank:    req.Bank,
		Account: req.Account,
	}

	if err := h.DirectMethodService.Update(ctx, req.ID, d); err != nil {
		zap.L().Error("server update direct method failed", zap.Error(err))
		e := apperrors.NewInternal()
		ctx.JSON(e.Status(), gin.H{
			"msg": e.Message,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": updatePaymentMethodSuccess,
	})
}
